﻿using System.Collections.Generic;
using System.Linq;

namespace CoreLims.Models.CoreLims
{
    using SiteGroup = CommonSiteGroup;

    public static class CommonSiteGroupExtension
    {
        /// <summary>
        /// 根据 实验室以及工作组名，获得对应的信息
        /// </summary>
        /// <param name="site"></param>
        /// <param name="group"></param>
        /// <returns></returns>
        public static SiteGroup getSiteGroup(this CommonSiteGroup sg, CoreLimsContext dbContext, string site, string group) =>
            dbContext.SiteGroup.FirstOrDefault(m => m.Site == site && m.WorkGroup == group);

        /// <summary>
        /// 根据 Id ，获得实验室和工作组
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public static SiteGroup getSiteGroupById(this CommonSiteGroup sg, CoreLimsContext dbContext, string Id) =>
            dbContext.SiteGroup.FirstOrDefault(m => m.Id == Id);

        /// <summary>
        /// 获得指定实验室的所有工作组列表
        /// </summary>
        /// <param name="Site"></param>
        /// <returns></returns>
        public static List<SiteGroup> getSiteGroup(this CommonSiteGroup sg, CoreLimsContext dbContext) =>
            dbContext.SiteGroup.OrderBy(s => s.Site).ToList();

        /// <summary>
        /// 获得实验室和工作组列表
        /// </summary>
        /// <returns></returns>
        public static List<SiteGroup> getSiteGroupList(this CommonSiteGroup sg, CoreLimsContext dbContext) =>
            dbContext.SiteGroup.ToList();

        /// <summary>
        /// 添加一个实验室和工作组
        /// </summary>
        /// <param name=""></param>
        public static string AddSiteGroup(this CommonSiteGroup sg, CoreLimsContext dbContext, CommonSiteGroup siteGroup)
        {
            CommonSiteGroup newsg = Common.SiteGroup.getSiteGroup(dbContext, siteGroup.Site, siteGroup.WorkGroup);
            if (newsg == null)
            {
                siteGroup.Id = Common._newId();
                dbContext.Add(siteGroup);
                dbContext.SaveChanges();
                return "实验室和工作组添加成功！";
            }
            else
                return "该实验室下已存在相同工作组名！";
        }

        /// <summary>
        /// 删除实验室和工作组
        /// </summary>
        /// <param name="Id"></param>
        public static string DeleteSiteGroup(this CommonSiteGroup sg, CoreLimsContext dbContext, string Id)
        {
            CommonSiteGroup siteGroup = Common.SiteGroup.getSiteGroupById(dbContext, Id);
            dbContext.Remove(siteGroup);
            dbContext.SaveChanges();
            return "删除成功！";
        }

    }
}
